/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package onpu_dms_xls.kernel.common.XML_Parsers;

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import onpu_dms_xls.kernel.common.inputParameters.ToDB_InputParameters;

/**
 *
 * @author snbm                       
 */
public class SAX_XML_2DB_InputParam_Parser extends DefaultHandler {

    private String tempVal;
    //to maintain context
    ToDB_InputParameters toDBip;


    public void runExample(String xmlFilePath, ToDB_InputParameters fDBip) throws IOException{
        this.toDBip = fDBip;
        parseDocument(xmlFilePath);
        return;
    }


    private void parseDocument(String xmlFilePath) throws IOException{

        //get a factory
        SAXParserFactory spf = SAXParserFactory.newInstance();
        try {

            //get a new instance of parser
            SAXParser sp = spf.newSAXParser();

            //parse the file and also register this class for call backs
            sp.parse(xmlFilePath, this);

        } catch (SAXException se) {
            throw new IOException(se.getMessage());
        } catch (ParserConfigurationException pce) {
            throw new IOException(pce.getMessage());
        }
    }

    //Event Handlers
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        //reset
        tempVal = "";

    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        tempVal = new String(ch, start, length);
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (qName.equals("documentClassId")){
            this.toDBip.setDocumentClassId(Integer.parseInt(tempVal));
        } else if (qName.equals("temporaryFolder")){
            this.toDBip.setTemporaryFolder(tempVal);
        }

    }
}